package NewTmas.Popupmenus;

import NewTmas.FramesandPanels.IntFrame;
import NewTmas.FramesandPanels.JobGraphPanel;
import NewTmas.FramesandPanels.JobMonitorOpenerFrame;
import NewTmas.Listeners.ChangeStatusActionListener;
import NewTmas.Listeners.HelpFileListener;
import NewTmas.Listeners.LogFileActionListener;
import NewTmas.Listeners.OpsPropsActionListener;
import taskmaster.controller.ViewTasks;
import taskmaster.job.Task;
import taskmaster.job.TaskCollection;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 7/22/11
 * Time: 5:22 PM
 * The main popup menu shown whenever you click the task node or task row in table view
 */
public class TaskPopupMenu extends JPopupMenu {

    String task;
    JobGraphPanel panel;
    HashMap<String,String> signoffMap;

    public TaskPopupMenu(final JobGraphPanel p, String task){

        this.task=task;
        this.panel=p;
        this.signoffMap=panel.signoffMap;



        TaskCollection taskCollection = panel.parentFrame.getTaskCollection();


        final JMenuItem signoff = new JMenuItem("Change  to signoff");
        JMenuItem retry = new JMenuItem("Change to retry");
        JMenuItem running = new JMenuItem("Change to running");
        JMenuItem succeeded = new JMenuItem("Change to succeeded");
        JMenuItem continue_anyway = new JMenuItem("Change to continue_anyway");
        JMenuItem failed_to_start = new JMenuItem("Change to failed_to_start");
        JMenuItem timed_out = new JMenuItem("Change to timed_out");
        JMenuItem failed = new JMenuItem("Change to failed");
        JMenuItem skipped = new JMenuItem("Change to skipped");
        JMenuItem review = new JMenuItem("Change to review");
        JMenuItem hold = new JMenuItem("Change to hold");
        JMenuItem not_yet_run = new JMenuItem("Change to not yet run");
        not_yet_run.addActionListener(new ChangeStatusActionListener(panel, -100, taskCollection, task));
        review.addActionListener(new ChangeStatusActionListener(panel, -6, taskCollection, task));
        hold.addActionListener(new ChangeStatusActionListener(panel, -8, taskCollection, task));
        failed.addActionListener(new ChangeStatusActionListener(panel, -4, taskCollection, task));
        timed_out.addActionListener(new ChangeStatusActionListener(panel, -3, taskCollection, task));
        failed_to_start.addActionListener(new ChangeStatusActionListener(panel, -2, taskCollection, task));
        retry.addActionListener(new ChangeStatusActionListener(panel, -1, taskCollection, task));
        running.addActionListener(new ChangeStatusActionListener(panel, 0, taskCollection, task));
        succeeded.addActionListener(new ChangeStatusActionListener(panel, 1, taskCollection, task));
        continue_anyway.addActionListener(new ChangeStatusActionListener(panel, 2, taskCollection, task));
        signoff.addActionListener(new ChangeStatusActionListener(panel, 3, taskCollection, task));
        skipped.addActionListener(new ChangeStatusActionListener(panel, 4, taskCollection, task));

        JMenuItem properties = new JMenuItem("Properties");
        JMenuItem openLog = new JMenuItem("Open Log File");



        Map<String, JMenuItem> possibleStatusDestinations = new HashMap<String, JMenuItem>();
        possibleStatusDestinations.put("-100", not_yet_run);
        possibleStatusDestinations.put("-6", review);
        possibleStatusDestinations.put("-8", hold);
        possibleStatusDestinations.put("-4", failed);
        possibleStatusDestinations.put("-3", timed_out);
        possibleStatusDestinations.put("-2", failed_to_start);
        possibleStatusDestinations.put("-1", retry);
        possibleStatusDestinations.put("0", running);
        possibleStatusDestinations.put("1", succeeded);
        possibleStatusDestinations.put("2", continue_anyway);
        possibleStatusDestinations.put("3", signoff);
        possibleStatusDestinations.put("4", skipped);

        /*adding Listeners for change status menu options*/
        for (Map.Entry<String, JMenuItem> menuItemEntry : possibleStatusDestinations.entrySet())
        {


            final Task taskObject = taskCollection.getTask(panel.job, task);
            //task could be filtered out of job and thus null
            if (taskObject != null && taskCollection.canMakeTransition(panel.user, taskObject, menuItemEntry.getKey()) )
            {
                add(menuItemEntry.getValue());
            }
        }

        add(new JSeparator());
        add(openLog);
        add(properties);

        properties.addActionListener(new OpsPropsActionListener(panel.job, task, taskCollection, panel.dtp));
        openLog.addActionListener(new LogFileActionListener(panel.job, task, panel.date, panel.dtp));

        ViewTasks vt=new ViewTasks();
        File f=null;
        try {
            f=  vt.findHelpFilePath(panel.job,task);

        } catch (Exception e1) {
            JOptionPane.showMessageDialog(null,e1.getMessage(),"Error finding help file.System Properties may not be set: ",JOptionPane.ERROR_MESSAGE);
            e1.printStackTrace();
        }
        if(f!=null){
            JMenuItem helpFile=new JMenuItem("Open Help");
            add(helpFile);
            helpFile.addActionListener(new HelpFileListener(f,panel.job, task, panel.date, taskCollection, panel.dtp,panel.user));

        } else
        {
            JMenuItem createHelp=new JMenuItem("Create a Help file") ;
            add(createHelp);
            createHelp.addActionListener(new HelpFileListener(panel.job, task, panel.date, taskCollection, panel.dtp,panel.user));

        }

        if(signoffMap.containsKey(panel.job+":"+task)){

            final String signoffSource=signoffMap.get(panel.job+":"+task)  ;
            JMenuItem signoffSourceItem=new JMenuItem("View "+signoffMap.get(panel.job+":"+task)+" Signoff Source "+task);
            add(signoffSourceItem);
            signoffSourceItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if(panel.lib.equalsIgnoreCase("GraphViz")){

                        JComboBox XDim=new JComboBox() ;
                        JComboBox YDim=new JComboBox();
                        for(int i: JobMonitorOpenerFrame.xdim){
                            XDim.addItem(i)  ;
                        }

                        for(int i:JobMonitorOpenerFrame.ydim) {

                            YDim.addItem(i);
                        }

                        Object message=    new Object[]{"X dimension",XDim,"Y Dimension",YDim};



                        JOptionPane.showConfirmDialog(null,message,"Select Graph dimensions",JOptionPane.DEFAULT_OPTION);
                        int graphWidth= Integer.parseInt(XDim.getSelectedItem().toString());
                        int graphHeight= Integer.parseInt(YDim.getSelectedItem().toString());
                        IntFrame nf=new IntFrame(new String[] {signoffSource}, panel.date, new String[0],new int[0], panel.lib,panel.user ,panel.tb,panel.dtp,panel.signoffMap,graphWidth,graphHeight) ;

                    } else{


                        IntFrame nf=new IntFrame(new String[] {signoffSource}, panel.date, new String[0],new int[0], panel.lib,panel.user ,panel.tb,panel.dtp,panel.signoffMap,0,0) ;
                    }
                }
            });
        }




    }


}
